package com.augurit.common.system.web.org;

import java.util.List;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;

import com.augurit.common.base.action.ExtCrudActionSupport;
import com.augurit.common.base.extmodel.base.BaseTreeNode;
import com.augurit.common.base.extmodel.inner.MenuCheckTreeNode;
import com.augurit.common.constant.OrgConstant;
import com.augurit.common.system.service.org.IAcMenuService;
import com.augurit.common.system.service.org.IAcRoleService;
import com.augurit.common.system.service.org.IAcUserShortcutService;
import com.augurit.common.system.web.auth.form.LoginUserForm;
import com.augurit.common.system.web.org.form.AcMenuForm;
import com.augurit.common.system.web.org.form.OmUserForm;
import com.augurit.common.util.ExtUtils;

/**
 * 菜单管理Action类
 */
@SuppressWarnings("serial")
@Results({
	@Result(name = ExtCrudActionSupport.RELOAD, location = "ac-menu.action", type = "redirect"),
	@Result(name = ExtCrudActionSupport.SUCCESS, location = "/common/system/org/ac-menu.jsp")
})
public class AcMenuAction extends ExtCrudActionSupport<AcMenuForm> {

	// Service 引用
	@Autowired
	private IAcMenuService acMenuService;
	@Autowired
	private IAcRoleService acRoleService;
	@Autowired
	private IAcUserShortcutService acUserShortcutService;
	
	
	// 基本属性
	private AcMenuForm form;
	private Long id;
	private Long[] ids;
	private Page<AcMenuForm> page = new Page<AcMenuForm>();
	private Long userId;
	

	/**
	 * 菜单列表查询
	 */
	public String list() throws Exception {
		ExtUtils.initPageFromExtGridParam(this.getRequest(), page);
		acMenuService.getAllMenus(page, form);
		this.extRenderGridJson(page);
		return null;
	}
	
	/**
	 * 获取系统中所有与功能点关联的菜单
	 * @return
	 * @throws Exception
	 */
	public String getAllMenusAssociateFunc() throws Exception {
		ExtUtils.initPageFromExtGridParam(this.getRequest(), page);
		acMenuService.getAllMenusAssociateFunc(page, form);
		this.extRenderGridJson(page);
		return null;
	}

	/**
	 * 获取菜单树指定树节点的子节点
	 */
	public String getChildren() throws Exception {
		
		List result = null;

		if(BaseTreeNode.isRootNode(id))
			result = acMenuService.getMenusByMenuLevel(1, OrgConstant.ZERO, BaseTreeNode.class.getSimpleName());
		else
			result = acMenuService.getChildTree(id, BaseTreeNode.class.getSimpleName());
		
		this.extRenderListJson(result, false);
		return null;
	}
	
	/**
	 * 获取当前用户菜单树
	 */
	public String getCurUserMenuTree() throws Exception {
		List result = null;

		if(BaseTreeNode.isRootNode(id))
			result = acMenuService.getMenusByMenuLevel(this.getLoginUser(), 1, OrgConstant.ZERO, BaseTreeNode.class.getSimpleName(), true);
		else
			result = acMenuService.getChildTree(this.getLoginUser(), id, 1 , BaseTreeNode.class.getSimpleName(), true);
		
		this.extRenderListJson(result, false);
		return null;
	}
	
	/**
	 * 根据用户的id构造LoginUserForm对象
	 * @param userId 用户id
	 * @return
	 */
	private LoginUserForm buildLoginUserForm(Long userId){
		if(userId != null){
			LoginUserForm loginUserform = new LoginUserForm();
			OmUserForm userForm = new OmUserForm();
			
			userForm.setUserId(userId);
			loginUserform.setUser(userForm);
			Long[] funcIds = acRoleService.getFuncIdsByUserId(userId);
			Long[] menuIds = acMenuService.getAllRelatedMenuIds(funcIds);
			loginUserform.setMenuIds(menuIds);
			return loginUserform;
		}
		return null;
	}
	/**
	 * 
	 * 获取某个用户具有权限的菜单check树
	 * @param userId 用户id
	 */
	public String getMenuCheckTreeByUser() throws Exception {
		List result = null;
		LoginUserForm loginUserform = this.buildLoginUserForm(userId);
		
		if(BaseTreeNode.isRootNode(id != null ? id.toString() : null))
			result = acMenuService.getMenusByMenuLevel(loginUserform, 1, OrgConstant.ZERO ,MenuCheckTreeNode.class.getSimpleName(), true);
		else
			result = acMenuService.getChildTree(loginUserform, id, 1, MenuCheckTreeNode.class.getSimpleName(), true);
		
		this.extRenderListJson(result, false);
		return null;
	}
	
	/**
	 * 获取菜单check树
	 */
	public String getMenuCheckTree() throws Exception {
		List result = null;
		
		if(BaseTreeNode.isRootNode(id != null ? id.toString() : null))
			result = acMenuService.getMenusByMenuLevel(1, OrgConstant.ZERO ,MenuCheckTreeNode.class.getSimpleName());
		else
			result = acMenuService.getChildTree(id, MenuCheckTreeNode.class.getSimpleName());
		
		this.extRenderListJson(result, false);
		return null;
	}
	
	
	/**
	 * 新增或编辑菜单页面
	 */
	@Override
	public String input() throws Exception {
		this.extRenderFormJson(form);
		return null;
	}
	
	/**
	 * 获取用户拥有的菜单
	 * @param userId 用户id
	 */
	public String getMenusOwnedByUser()throws Exception {
		ExtUtils.initPageFromExtGridParam(this.getRequest(), page);
		if(userId != null){
			Long[] menuIds = acMenuService.getMenuIdsOwnedByUser(userId);
			acMenuService.getMenusInMenuIds(page, menuIds);
			this.extRenderGridJson(page);
			return null;
		}
		return this.list();
	}
	
	/**
	 * 保存菜单信息
	 */
	@Override
	public String save() throws Exception {
		acMenuService.saveCascade(form);
		this.extRenderSuccess();
		return null;
	}

	/**
	 * 删除一个或多个菜单记录
	 */
	@Override
	public String delete() throws Exception {
		acMenuService.deleteMenuCasCade(ids);
		this.extRenderSuccess();
		return null;
	}

	
	@Override
	public void prepare() throws Exception {
	}
	
	public void prepareGetAllMenusAssociateFunc() throws Exception {
		prepareModel();
	}
	
	public void prepareList() throws Exception {
		prepareModel();
	}
	
	public void prepareInput() throws Exception {
		prepareModel();
	}
	
	public void prepareSave() throws Exception {
		prepareModel();
	}
	
	
	public AcMenuForm getModel() {
		return form;
	}
	
	@Override
	protected void prepareModel() throws Exception {
		if(id != null)
			form = acMenuService.get(id);
		else
			form = new AcMenuForm();
	}
	
	
	// 属性getter和setter方法
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public AcMenuForm getForm() {
		return form;
	}

	public void setForm(AcMenuForm form) {
		this.form = form;
	}

	public Long[] getIds() {
		return ids;
	}

	public void setIds(Long[] ids) {
		this.ids = ids;
	}

	public Long getUserId() {
		return userId;
	}

	public void setUserId(Long userId) {
		this.userId = userId;
	}
}
